package com.turtle3d.user.visualizators;

import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Material;
import javax.vecmath.Color3f;

import com.turtle3d.formallanguage.VisualSymbol;
import com.turtle3d.formallanguage.symbol.Symbol;
import com.turtle3d.visualizator.MyPyramid;
import com.turtle3d.visualizator.VisualData;
import com.turtle3d.visualizator.Visualizator;

public class Pyramid extends Visualizator {

	@Override
	public BranchGroup visualize(VisualData visualData) {
		// Fractal's root
		BranchGroup branchGroup = new BranchGroup();

		ArrayList<VisualSymbol> visualSequence = visualData.getVisualSequence();

		// Iterate over visual symbols sequence
		for (int i = 0; i < visualSequence.size(); i++) {

			VisualSymbol visualSymbol = visualSequence.get(i);

			// Only "B" symbol is visualized
			if (!visualSymbol.getSymbol().equals(new Symbol("B")))
				continue;

			// Pyramid side length is equal to turtle's step
			Float a = visualSymbol.getSymbol().getParameter(Symbol.DISTANCE);
			// Depth the number of forward moves of the turtle. Pushes and pops
			// are
			// taken under consideration.
			int depth = visualSymbol.getDepth();
			float normalizedDepth = depth
					/ (visualData.getMaximalDepthReached() * 1f);

			MyPyramid pyramid = new MyPyramid(a);
			// Name may be important after the export
			pyramid.setName("Pyramid");

			// Java3D way of setting up object's appearance
			// Color changes with depth
			Appearance appearance = new Appearance();
			Material m = new Material();
			m.setDiffuseColor(new Color3f(1f, normalizedDepth, 0f));
			appearance.setMaterial(m);
			pyramid.setAppearance(appearance);

			// Transform and add pyramid to scene
			branchGroup.addChild(createTransformGroup(visualSymbol
					.getTransform(), pyramid));

		}

		return branchGroup;
	}

	public void setRequiredParameters() {

	}

}
